#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<string>
#include<functional>
#include<cerrno>
#include<cstring>
#include<netinet/in.h>//网络需要的结构体
#include <arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
using namespace std;
enum
{
    SOCKET_ERR=1,
    BIND_ERR=2,
    
};
class udpclient 
{public:
    udpclient(string ip,uint16_t port):_sockfd(-1),_serverport(port),_serverip(ip)
    {}
    ~udpclient(){}
    void initclient()
    {   _sockfd=socket(AF_INET,SOCK_DGRAM,0);//创建套接字
        if(_sockfd==-1)
        {
            cerr<<"socket error:"<<errno<<":"<<strerror(errno)<<endl;
            exit(SOCKET_ERR);
        }
        cout<<"create success"<<endl;
    }
    void run()
    {   char buffer[1024];
        struct sockaddr_in server;
        bzero(&server,sizeof(server));
        server.sin_family=AF_INET;
       server.sin_addr.s_addr=inet_addr(_serverip.c_str());
       server.sin_port=htons(_serverport);
      int len=sizeof(server);
      string message;
        while(true)
        {
          
          fprintf(stderr,"enter");//不懂需要学习
          fflush(stderr);
          fgets(buffer,sizeof(buffer),stdin);
          buffer[strlen(buffer)-1]=0;
           message=buffer;
          sendto(_sockfd,message.c_str(),message.size(),0,(struct sockaddr*)&server,sizeof(server));

        }



    }


private:
   int _sockfd;
   string _serverip;
   uint16_t _serverport;
    
};